﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Things.Common;
using LTP.Accounts.Bus;
using System.IO;
namespace Things.Web.CropsPlantProgressInfo
{
    public partial class Add : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCropsPlantProgress();

                if (Request.Params["CropsID"] != null && Request.Params["CropsID"].Trim() != "")
                {
                    Guid cropsID = Guid.Empty;
                    Guid.TryParse(Request.Params["CropsID"], out cropsID);
                    if (cropsID != Guid.Empty)
                    {
                        btnGoCrops.Visible = true;
                    }
                }
                else
                {
                    btnGoCrops.Visible = false;
                }
            }
        }

        protected void BindCropsPlantProgress()
        {
            Things.BLL.CropsPlantProgress cppBLL = new BLL.CropsPlantProgress();
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append(" IsDeleted=0 ");
            ds = cppBLL.GetList(-1, strWhere.ToString(), "PlantProgressOrder");
            Things.Common.UICommon.BindDDL(ddlPlantProgress, ds, "PlantProgressName", "PlantProgressID");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (!PageValidate.IsDateTime(txtPhotoDatetime.Text))
            {
                strErr += "照片日期格式错误！\\n";
            }
            if (!PageValidate.IsDateTime(txtCreateDatetime.Text))
            {
                strErr += "记录时间格式错误！\\n";
            }
            if (this.ddlPlantProgress.SelectedValue == "-1")
            {
                strErr += "请选择种植过程类型！\\n";
            }

            Guid cppID = Guid.Empty;
            if (Guid.TryParse(this.ddlPlantProgress.SelectedValue, out cppID)==false)
            {
                strErr += "请选择种植过程类型！\\n";
            }

            Guid cropsID = Guid.Empty;
            Guid.TryParse(Request.Params["CropsID"], out cropsID);
            if (cropsID == Guid.Empty)
            {
                strErr += "农产品信息有误！\\n";
            }

         

            byte[] Image = Things.Common.UICommon.GetImages(FileUpload1);

            if (string.IsNullOrEmpty(FileUpload1.FileName) == false && Image.Length <= 0)
            {
                strErr += "图片格式不正确！\\n";
            }


            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }

            
            DateTime PhotoDatetime = DateTime.Parse(this.txtPhotoDatetime.Text);
            DateTime CreateDatetime = DateTime.Parse(this.txtCreateDatetime.Text);
            bool IsDeleted = false;
            string Comments = this.txtComments.Text;

            Things.Model.CropsPlantProgressInfo model = new Things.Model.CropsPlantProgressInfo();
            model.CropsID = cropsID;
            model.PlantProgressID = cppID;
            model.Image = Image;
            model.PhotoDatetime = PhotoDatetime;
            model.CreateDatetime = CreateDatetime;
            model.IsDeleted = IsDeleted;
            model.Comments = Comments;

            Things.BLL.CropsPlantProgressInfo bll = new Things.BLL.CropsPlantProgressInfo();
            bll.Add(model);
            Things.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "add.aspx");

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }

        protected void btnGoCrops_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Crops/list.aspx");
        }
    }
}
